package com.example.demo.service.impl;

import com.example.demo.Util.Result;
import com.example.demo.Util.ResultUtil;
import com.example.demo.mapper.CollegeMap;
import com.example.demo.po.College;
import com.example.demo.service.CollegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.List;

@Service
public class CollegeServiceImpl implements CollegeService {

    @Autowired
    CollegeMap collegeMap;

    @Override
    public Result SaveCollege(College college) {

        if(StringUtils.isEmpty( college.getName())){
            return ResultUtil.fail("缺少名字参数");
        }


        List<College> collegeList = collegeMap.SearchCollegeByName(college.getName());

        // 验证是否存在同名
        if(collegeList.size()>0&&collegeList.get(0).getId()!=college.getId()){
            return ResultUtil.fail("有重复名称的学院");
        }

        collegeMap.SaveCollege(college);
        return ResultUtil.success(college);
    }

    @Override
    public Result UpdateCollege(College college) {
        if(StringUtils.isEmpty( college.getName())){
            return ResultUtil.fail("缺少名字参数");
        }

        List<College>  collegeList = collegeMap.SearchCollegeByName(college.getName());

        // 验证是否存在同名
        if(collegeList.size()>0&&collegeList.get(0).getId()!=college.getId()){
            return ResultUtil.fail("有重复名称的学院");
        }
        collegeMap.UpdateCollege(college);
        return ResultUtil.success("更新成功");
    }

    @Override
    public Result DeleteCollege(College college) {
        return null;
    }
}
